package com.kzd.gtgame.main.game.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.TimeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.kzd.gtgame.R
import com.kzd.gtgame.entity.*
import com.kzd.gtgame.main.checkLogin
import com.kzd.gtgame.main.toGameDetailsActivity
import com.kzd.gtgame.main.toGameH5Activity
import com.kzd.gtgame.main.toTransferDetailsActivity
import com.module.lemlin.image.loadImage
import com.youth.banner.adapter.BannerAdapter
import java.lang.StringBuilder

class Game1Adapter<T : GameBean>(var type: Type = Type.GAME_LIST_DEFAULT) :
    BaseQuickAdapter<T, BaseViewHolder>(R.layout.view_game_1) {

    enum class Type {
        GAME_LIST_DEFAULT, GAME_LIST_CLASSIFY, GAME_LIST_TRANSFER, GAME_LIST_RANKING_0, GAME_LIST_RANKING_1, GAME_LIST_RANKING_2
    }

    init {

        addChildClickViewIds(R.id.tvGame1Action)

        setOnItemClickListener { _, _, position ->
            val gid = data[position].id
                ?: return@setOnItemClickListener
            if (type == Type.GAME_LIST_TRANSFER) {
                toTransferDetailsActivity(gid)
            } else {
                toGameDetailsActivity(gid)
            }
        }

        setOnItemChildClickListener { _, view, position ->
            val itemData = getItem(position)
            if (view.id != R.id.tvGame1Action)
                return@setOnItemChildClickListener
            val gid = itemData.id
                ?: return@setOnItemChildClickListener
            when {
                itemData.system_type == "2" -> {
                    toGameH5Activity(itemData.name, itemData.icon, gid)
                }
                type == Type.GAME_LIST_TRANSFER -> {
                    toTransferDetailsActivity(gid)
                }
                else -> toGameDetailsActivity(gid)
            }
        }
    }

    override fun convert(holder: BaseViewHolder, item: T) {

        holder.getView<FrameLayout>(R.id.flGame1Serial)
            .visibility = if (type == Type.GAME_LIST_RANKING_0 ||
            type == Type.GAME_LIST_RANKING_1 ||
            type == Type.GAME_LIST_RANKING_2
        ) View.VISIBLE else View.GONE

        holder.getView<View>(R.id.vwGame1Divider)
            .visibility = View.VISIBLE

        holder.getView<ImageView>(R.id.ivGame1SerialIcon).apply {
            if (type == Type.GAME_LIST_RANKING_1 || type == Type.GAME_LIST_RANKING_2) {
                val icon = when (holder.layoutPosition - headerLayoutCount) {
                    0 -> R.mipmap.weektop_one_icon
                    1 -> R.mipmap.weektop_two_icon
                    2 -> R.mipmap.weektop_three_icon
                    else -> 0
                }
                setImageResource(icon)
            } else setImageResource(0)
        }

        holder.getView<TextView>(R.id.tvGame1SerialText).apply {

            val position = holder.layoutPosition - headerLayoutCount
            text = if (type == Type.GAME_LIST_RANKING_0) {
                "${position + 4}"
            } else if (type == Type.GAME_LIST_RANKING_1 || type == Type.GAME_LIST_RANKING_2) {
                if (position < 3)
                    null else "${position + 1}"
            } else null

            val count = if (type == Type.GAME_LIST_RANKING_0)
                position + 4 else position + 1
            textSize = if (count > 99) 12f else 15f
        }

        holder.getView<TextView>(R.id.tvGame1Action).apply {
            visibility = if (type == Type.GAME_LIST_CLASSIFY)
                View.GONE else View.VISIBLE
            text = if (type == Type.GAME_LIST_TRANSFER)
                "转游" else if (type == Type.GAME_LIST_RANKING_2 || item.system_type == "2") "打开" else "下载"
        }

        holder.setText(R.id.tvGame1Name, item.name)
            .setText(R.id.tvGame1Rebate, item.zhekou)
            .setGone(R.id.tvGame1Rebate, item.zhekou.isNullOrEmpty())
            .setText(
                R.id.tvGame1Type,
                StringBuilder().append(item.type).append("\t·\t").apply {
                    if (item.system_type == "2") append("H5") else append(item.size).append("MB")
                }
            )
            .getView<ImageView>(R.id.ivGame1Icon)
            .loadImage(
                src = item.icon ?: R.mipmap.ic_80008,
                placeholder = R.mipmap.ic_80008,
                error = R.mipmap.ic_80008
            )

        holder.getView<RecyclerView>(R.id.rvGame1LabelBody).apply {
            layoutManager = LinearLayoutManager(context).apply {
                orientation = LinearLayoutManager.HORIZONTAL
            }
            isNestedScrollingEnabled = false
        }.adapter = GameListLabelAdapter().apply {
            setNewInstance(item.biaoqian.toMutableList())
        }
    }
}

class Game2Adapter : BaseQuickAdapter<GameBean, BaseViewHolder>(R.layout.view_game_2) {
    init {
        setOnItemClickListener { _, _, position ->
            val gid = getItem(position).id
                ?: return@setOnItemClickListener
            toGameDetailsActivity(gid)
        }
    }

    override fun convert(holder: BaseViewHolder, item: GameBean) {
        val label = StringBuilder()
        item.biaoqian.forEach {
            label.append(it).append("·")
        }
        if (label.isNotEmpty()) {
            label.deleteAt(label.lastIndex)
        }
        holder.setText(R.id.tvGame2Name, item.name)
            .setText(R.id.tvGame2Label, label)
            .getView<ImageView>(R.id.ivGame2Icon)
            .loadImage(src = item.icon ?: R.mipmap.ic_80008, R.mipmap.ic_80008, R.mipmap.ic_80008)
    }
}

class Game3Adapter :
    BaseQuickAdapter<GameBean, BaseViewHolder>(R.layout.view_game_3) {
    init {
        setOnItemClickListener { _, _, position ->
            val gid = getItem(position).id
                ?: return@setOnItemClickListener
            toGameDetailsActivity(gid)
        }
    }

    override fun convert(holder: BaseViewHolder, item: GameBean) {
        holder.setText(R.id.ivGame3Name, item.name)
            .setText(R.id.tvGame3label, item.keywords)
            .getView<ImageView>(R.id.ivGame3Icon)
            .loadImage(src = item.icon ?: R.mipmap.ic_80008, R.mipmap.ic_80008, R.mipmap.ic_80008)
        holder.getView<TextView>(R.id.tvGame3label).apply {
            visibility = if (item.keywords.isNullOrEmpty())
                View.GONE else View.VISIBLE
        }
    }
}

class Game4Adapter(banners: MutableList<GameBanner>) :
    BannerAdapter<GameBanner, com.kzd.gtgame.base.BaseViewHolder>(banners) {
    override fun onCreateHolder(
        parent: ViewGroup?, viewType: Int
    ): com.kzd.gtgame.base.BaseViewHolder {
        val view = LayoutInflater.from(parent?.context)
            .inflate(R.layout.view_game_4, parent, (false))
        view.layoutParams = FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
        )
        return com.kzd.gtgame.base.BaseViewHolder(view)
    }

    override fun onBindView(
        holder: com.kzd.gtgame.base.BaseViewHolder?,
        data: GameBanner, position: Int, size: Int
    ) {
        val itemView = holder?.itemView
            ?: return
        itemView.findViewById<ImageView>(R.id.ivGame4Image)
            ?.loadImage(src = data.image ?: R.mipmap.ic_80004, R.mipmap.ic_80004, R.mipmap.ic_80004)
        itemView.findViewById<TextView>(R.id.tvGame4Name)
            ?.text = data.game_name
        itemView.findViewById<TextView>(R.id.tvGame4Type)
            ?.text = StringBuilder().append(data.type)
            .append("\t·\t").append(data.size).append("MB")
        itemView.findViewById<RecyclerView>(R.id.rvGame4Label)?.apply {
            layoutManager = LinearLayoutManager(context).apply {
                orientation = LinearLayoutManager.HORIZONTAL
            }
        }?.adapter = GameListLabelAdapter(style = 1).apply {
            setNewInstance(data.biaoqian?.toMutableList()?: mutableListOf())
        }

    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        mRecyclerView = recyclerView
    }

    private var mRecyclerView: RecyclerView? = null

    var recyclerView: RecyclerView
        set(value) {
            mRecyclerView = value
        }
        get() {
            checkNotNull(mRecyclerView) {
                "Please get it after onAttachedToRecyclerView()"
            }
            return mRecyclerView!!
        }

}

class Game5Adapter : BaseQuickAdapter<GameStart, BaseViewHolder>(R.layout.view_game_5) {
    init {
        setOnItemClickListener { _, _, position ->
            val gid = getItem(position).id
                ?: return@setOnItemClickListener
            toGameDetailsActivity(gid)
        }
    }

    override fun convert(holder: BaseViewHolder, item: GameStart) {
        val label = StringBuilder()
        item.biaoqian.forEach {
            label.append(it).append("·")
        }
        if (label.isNotEmpty()) {
            label.deleteAt(label.lastIndex)
        }
        holder.setText(R.id.tvGame5Name, item.name)
            .setText(R.id.tvGame5Label, label)
            .setText(R.id.tvGame5Time, item.shoufa_time)
            .getView<ImageView>(R.id.ivGame5Image)
            .loadImage(src = item.image1 ?: R.mipmap.ic_80008, R.mipmap.ic_80008, R.mipmap.ic_80008)
    }
}

class Game6Adapter(games: MutableList<GameReserve>) :
    BannerAdapter<GameReserve, com.kzd.gtgame.base.BaseViewHolder>(games) {
    override fun onCreateHolder(
        parent: ViewGroup?, viewType: Int
    ): com.kzd.gtgame.base.BaseViewHolder {
        val view = LayoutInflater.from(parent?.context)
            .inflate(R.layout.view_game_6, parent, false)
        view.layoutParams = FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        ).apply {
            this.bottomMargin = ConvertUtils.dp2px(12f)
            this.topMargin = ConvertUtils.dp2px(10f)
        }

        return com.kzd.gtgame.base.BaseViewHolder(view)
    }

    override fun onBindView(
        holder: com.kzd.gtgame.base.BaseViewHolder?,
        data: GameReserve, position: Int, size: Int
    ) {
        val itemView = holder?.itemView
            ?: return
        itemView.findViewById<ImageView>(R.id.ivGame6Image)
            ?.loadImage(src = data.image ?: R.mipmap.ic_80003, R.mipmap.ic_80003, R.mipmap.ic_80003)
        itemView.findViewById<ImageView>(R.id.ivGame6Icon)
            ?.loadImage(src = data.icon ?: R.mipmap.ic_80008, R.mipmap.ic_80003, R.mipmap.ic_80008)
        itemView.findViewById<TextView>(R.id.tvGame6Name)?.text = data.name
        val time = TimeUtils.millis2String((data.shangjia * 1000), ("MM-dd HH:mm"))
        itemView.findViewById<TextView>(R.id.tvGame6Time)?.text =
            StringBuilder().append(time).append("首发")
        itemView.findViewById<TextView>(R.id.tvGame6Reserve)?.apply {
            text = if (data.is_yuyue) "已预约" else "预约"
        }?.setOnClickListener {
            mOnItemReserve?.onReserve(it, position, data)
        }
    }

    fun setData(position: Int, data: GameReserve) {
        this.mDatas[position] = data
        notifyItemChanged(position)
    }


    private var mOnItemReserve: OnItemReserve? = null

    fun setOnItemReserve(onItemReserve: OnItemReserve) {
        mOnItemReserve = onItemReserve
    }

    interface OnItemReserve {
        fun onReserve(view: View, position: Int, reserve: GameReserve)
    }
}

class Game7Adapter :
    BaseQuickAdapter<GameBean, BaseViewHolder>(R.layout.view_game_7) {

    init {
        setOnItemClickListener { _, _, position ->
            val gid = getItem(position).id
                ?: return@setOnItemClickListener
            toGameDetailsActivity(gid)
        }
    }

    override fun convert(holder: BaseViewHolder, item: GameBean) {
        holder.setText(R.id.tvGame7Name, item.name)
            .setText(R.id.tvGame7Type, item.type)
            .getView<ImageView>(R.id.ivGame7Icon)
            .loadImage(src = item.icon ?: R.mipmap.ic_80008, R.mipmap.ic_80008, R.mipmap.ic_80008)

        holder.getView<ImageView>(R.id.ivGame7Serial).apply {
            val icon = when (holder.layoutPosition) {
                0 -> R.mipmap.yi_icon
                1 -> R.mipmap.er_icon
                2 -> R.mipmap.san_icon
                else -> 0
            }
            setImageResource(icon)
        }

        holder.getView<TextView>(R.id.tvGame7Serial).apply {
            val position = holder.layoutPosition + 1
            text = if (position < 4) null else position.toString()
        }
    }
}